/***************************************************************************
 创建者：       hualei
 开始时间：    2018.09.10
 copyright:(C) 深圳市华友高科有限公司
 修改说明：（每次有修改就添加一条，带有 修改人，修改时间，修改描述）

 ***************************************************************************/
#include "movefreeinstructiondelegate.h"
#include <QDebug>
#include "../../mapView/mapview.h"
#include "../../programView/programview.h"
#include "../../CommunicateDelegate/globaldata.h"
#include <QMessageBox>
#include "../../CommunicateDelegate/communicatenormaldelegate.h"
#include "instructionEnum.h"
#include "hmicommondefine.h"
#include "programregexp.h"

MoveFreeInstructionDelegate::MoveFreeInstructionDelegate(MapView *mapViewIn, GlobalData *globalDataIn,
                                            CommunicateNormalDelegate *communicateNormalDelegateIn,
                                                         QWidget *parent) :
    QObject(parent)
{
    mapView=mapViewIn;
    globalData=globalDataIn;
    communicateNormalDelegate=communicateNormalDelegateIn;

    programRegExp=new ProgramRegExp;
}

void MoveFreeInstructionDelegate::initView()
{
    moveFreeInstructionView->pointType = E_MOVE_FREE_POINTS_ARRY_DP;
    moveFreeInstructionView->navigationMode = E_NAVIGATION_TYPE_WHEEL;
    moveFreeInstructionView->singlePointValue = 1;
    moveFreeInstructionView->registerPointValue = 1;
    moveFreeInstructionView->trackFile = "";
    moveFreeInstructionView->velocityRatio = 0;
    moveFreeInstructionView->annotation = "(no Annotation!)";
    moveFreeInstructionView->moveFreeMoveTypeView->cntValue = 0;
    moveFreeInstructionView->moveFreeMoveTypeView->allowAccuracy = 0.1;
    moveFreeInstructionView->moveFreeMoveTypeView->moveFreeMoveTrajectoryType = E_MOVE_FREE_MOVE_TRAJECTORY_LINE;
    moveFreeInstructionView->moveFreeMoveType = E_MOVE_FREE_MOVE_P;

    moveFreeInstructionView->moveFreeVmarkSettingView->laserTypeNum = 0;
    moveFreeInstructionView->moveFreeVmarkSettingView->jmpLbValue = 1;

}

void MoveFreeInstructionDelegate::finishEditInstruction_Slot()
{
    moveFreeInstructionView->instruction = "MOVE_FREE ";
    QString str;
    if(moveFreeInstructionView->pointType == E_MOVE_FREE_POINTS_ARRY_DP)
    {
        // 从属性界面的点位索引加入容器
        //dpointIndexList << moveFreeInstructionView->singlePointValue;
        str = tr(" DP[") + QString("%1").arg(moveFreeInstructionView->singlePointValue) + "] ";
    }
    else if(moveFreeInstructionView->pointType == E_MOVE_FREE_POINTS_ARRY_DPR)
    {
        str = tr(" DPR[") + QString("%1").arg(moveFreeInstructionView->registerPointValue) + "] ";
    }
    else if(moveFreeInstructionView->pointType == E_MOVE_FREE_POINTS_ARRY_DPL)
    {
        str = tr(" DPL[") + moveFreeInstructionView->trackFile + "] ";
    }

    moveFreeInstructionView->instruction += (str + QString("%1").arg(moveFreeInstructionView->velocityRatio) + "%,");

    if(moveFreeInstructionView->navigationMode == E_NAVIGATION_TYPE_WHEEL)
    {
        str = tr("WHEEL,");
    }
    else if(moveFreeInstructionView->navigationMode == E_NAVIGATION_TYPE_IMU)
    {
        str = tr("IMU,");
    }
    else if(moveFreeInstructionView->navigationMode == E_NAVIGATION_TYPE_LASER)
    {
        str = tr("LASER,");
    }
    moveFreeInstructionView->instruction += str;

    if(moveFreeInstructionView->moveFreeMoveTypeView->cntValue == 0)
    {
        moveFreeInstructionView->instruction += tr("FINE");
    }
    else
    {
         moveFreeInstructionView->instruction += tr("CNT") + QString("%1").arg(moveFreeInstructionView->moveFreeMoveTypeView->cntValue);
    }

    if(moveFreeInstructionView->moveFreeMoveTypeView->moveFreeMoveTrajectoryType == E_MOVE_FREE_MOVE_TRAJECTORY_AUTO)
    {
        moveFreeInstructionView->instruction += tr(",AUTO");
    }
    else if(moveFreeInstructionView->moveFreeMoveTypeView->moveFreeMoveTrajectoryType == E_MOVE_FREE_MOVE_TRAJECTORY_LINE)
    {
        moveFreeInstructionView->instruction += tr(",LINE");
    }
    else if(moveFreeInstructionView->moveFreeMoveTypeView->moveFreeMoveTrajectoryType == E_MOVE_FREE_MOVE_TRAJECTORY_SPLINE)
    {
        moveFreeInstructionView->instruction += tr(",SPLINE");
    }
    else if(moveFreeInstructionView->moveFreeMoveTypeView->moveFreeMoveTrajectoryType == E_MOVE_FREE_MOVE_TRAJECTORY_ROTATE)
    {
        moveFreeInstructionView->instruction += tr(",ROTATE");
    }
    else
    {
        qDebug()<<"error, unknow moveFreeMoveTrajectoryType";
    }

    if(moveFreeInstructionView->moveFreeMoveType == E_MOVE_FREE_MOVE_N)
    {
        moveFreeInstructionView->instruction += tr(",N");
    }
    else if(moveFreeInstructionView->moveFreeMoveType == E_MOVE_FREE_MOVE_P)
    {
        moveFreeInstructionView->instruction += tr(",P");
    }
    // 停止精度
    moveFreeInstructionView->instruction += tr(",") + QString::number(moveFreeInstructionView->moveFreeMoveTypeView->allowAccuracy);

    // Point Type
    // 普通点
    if(moveFreeInstructionView->moveFreeVmarkSettingView->pointType == EM_MOVE_FREE_DP)
    {
        moveFreeInstructionView->instruction += tr(",") + QString("%1").arg(tr("OP"));
    }
    // 二维码点
    else if(moveFreeInstructionView->moveFreeVmarkSettingView->pointType == EM_MOVE_FREE_QR)
    {
        moveFreeInstructionView->instruction += tr(",") + QString("%1%2%3").arg(tr("QR[")).arg(moveFreeInstructionView->moveFreeVmarkSettingView->typeIndex).arg("]");
    }
    // VMark点
    else if(moveFreeInstructionView->moveFreeVmarkSettingView->pointType == EM_MOVE_FREE_VMARK)
    {
        moveFreeInstructionView->instruction += tr(",") + QString("%1%2%3").arg(tr("VMark[")).arg(moveFreeInstructionView->moveFreeVmarkSettingView->typeIndex).arg("]");
    }
    else if(moveFreeInstructionView->moveFreeVmarkSettingView->pointType == EM_MOVE_FREE_CU1)
    {
        moveFreeInstructionView->instruction += tr(",") + QString("%1").arg(tr("CU1"));
    }

    // LaserType
    moveFreeInstructionView->instruction += tr(",") + QString("%1%2%3").arg(tr("Laser[")).arg(moveFreeInstructionView->moveFreeVmarkSettingView->laserTypeNum).arg("]");

    //DET
    if(moveFreeInstructionView->moveFreeMoveTypeView->detectBarrierState == E_MOVE_FREE_DETECT_BARRIER_YES)
    {
        moveFreeInstructionView->instruction += tr(",") + tr("DET")+
                QString::number(moveFreeInstructionView->moveFreeMoveTypeView->barrierRunMode);
    }
    else
    {
        moveFreeInstructionView->instruction += tr(",") + tr("NODET");
    }

    // JMP LB[1]
    moveFreeInstructionView->instruction += tr(",") + tr("JMP LB[") + moveFreeInstructionView->moveFreeVmarkSettingView->jmpLbValue + tr("]");

    if(moveFreeInstructionView->moveFreeVmarkSettingView->pathControlStrategy == E_PATH_CONTROL_STRATEGY_MCCRUM)
    {
        moveFreeInstructionView->instruction += tr(",") + QString("%1").arg(tr("OMNI"));
    }

    moveFreeInstructionView->instruction += tr("  //") + moveFreeInstructionView->annotation;

    qDebug() << "finishedEdit MOVE_FREE instruction:" << moveFreeInstructionView->instruction;

//    qDebug() << "cntValue :" << moveFreeInstructionView->moveFreeMoveTypeView->cntValue;
//    qDebug() << "moveFreeMoveTrajectoryType :" << moveFreeInstructionView->moveFreeMoveTypeView->moveFreeMoveTrajectoryType;
//    qDebug() << "allowAccuracy :" << moveFreeInstructionView->moveFreeMoveTypeView->allowAccuracy;
//    qDebug() << "validLaserRange_moving :" << moveFreeInstructionView->moveFreeMoveTypeView->validLaserRange_moving;
//    qDebug() << "validLaserRange_stopping :" << moveFreeInstructionView->moveFreeMoveTypeView->validLaserRange_stopping;
}

void MoveFreeInstructionDelegate::clearNote()
{
    moveFreeInstructionView->annotation.clear();
    qDebug() << "Clear Note";
}

void MoveFreeInstructionDelegate::moveFreeTrackFileSlot(QString trackFilePath, QModelIndex modeIndex)
{
    Q_UNUSED(modeIndex);
    moveFreeInstructionView->trackFile = trackFilePath;
    moveFreeInstructionView->updateView();
}

void MoveFreeInstructionDelegate::recordDPointSlot(int recordType)
{
    // 清除，只保存一个点位数据
//    fileData.vehicleDPointList.clear();
    DPoint dpoint;
    VehiclePosition positionReturn;
    QString tmpPointType;
    moveFreeInstructionView->getInput();

    // 获取指令DP索引
    if(recordType == E_RECORD_MAP_SELECTI_POINT || recordType == E_RECORD_PROGRAM_VIEW_POINT)
    {
        QString tmpInstructionStr=moveFreeInstructionView->programView->getCurrentSelectionInstruction();
        if(MOVE_FREE!=programRegExp->patExtractInstructionType(tmpInstructionStr))
        {
            emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("载入点失败!\n选中的指令不支持载入点!"));
            return ;
        }
        Line lineOut;
        programRegExp->patMoveFree(tmpInstructionStr,lineOut);

            qDebug() << "Current Record Instruction :" << moveFreeInstructionView->programView->getCurrentSelectionInstruction();
            // 匹配指令获取点位索引

            dpoint.dpIndex = lineOut.moveFreeInstruction.pointsValue;


            if(EM_MOVE_FREE_VMARK==lineOut.moveFreeInstruction.pointSensorType)
            {
                dpoint.pointType = EM_MOVE_FREE_VMARK;
                tmpPointType = tr("VMark点");
            }
            else if(EM_MOVE_FREE_QR==lineOut.moveFreeInstruction.pointSensorType)
            {
                dpoint.pointType = EM_MOVE_FREE_QR;
                tmpPointType = tr("QR二维码点");
            }
            else if(EM_MOVE_FREE_DP==lineOut.moveFreeInstruction.pointSensorType)
            {
                dpoint.pointType = EM_MOVE_FREE_DP;
                tmpPointType = tr("OP普通点");
            }
            else if(EM_MOVE_FREE_CU1==lineOut.moveFreeInstruction.pointSensorType)
            {
                dpoint.pointType = EM_MOVE_FREE_CU1;
                tmpPointType = tr("CU1点");
            }

            // 指令程序界面选中指令为自由导航指令
            // 载入地图点位
            if(recordType == E_RECORD_MAP_SELECTI_POINT)
            {
                if(dpoint.pointType == EM_MOVE_FREE_DP || dpoint.pointType == EM_MOVE_FREE_CU1 )
                {
                    #ifdef D_FOR_AGV
                    // 获取地图选择点位
                    positionReturn = mapView->getCurrentSeletPosision();
                    dpoint.posX = positionReturn.x;
                    dpoint.posY = positionReturn.y;
                    // 地图点已做转换
                    dpoint.posR = positionReturn.rotate;
                    #endif

                }
                else
                {
                    emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("DP地图点位载入失败!\n地图点不能载入该指令!"));
                    return;
                }
            }
            // 指令列表界面载入
            else if(recordType == E_RECORD_PROGRAM_VIEW_POINT)
            {
                // 普通点
                if(dpoint.pointType == EM_MOVE_FREE_DP)
                {
                    // 不使用VMark
//                    globalData->lock();
                    if(globalData->vehicleInWorld.isUpdateSucess)
                    {
                        dpoint.posX = globalData->vehicleInWorld.position.x;
                        dpoint.posY = globalData->vehicleInWorld.position.y;
                        // 将角度单位由 度转换为弧度
                        dpoint.posR = globalData->vehicleInWorld.position.rotate/M_PI*180;
                    }
                    else
                    {
//                        globalData->unlock();
                        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("DP普通点位载入失败!\n数据获取失败!"));
                        return;
                    }
//                    globalData->unlock();

                }
                // CU1点
                else if(dpoint.pointType == EM_MOVE_FREE_CU1)
                {
                    // 不使用VMark
//                    globalData->lock();
                    if(globalData->vehicleInCU1.isUpdateSucess)
                    {
                        dpoint.posX = globalData->vehicleInCU1.position.x;
                        dpoint.posY = globalData->vehicleInCU1.position.y;
                        // 将角度单位由 度转换为弧度
                        dpoint.posR = globalData->vehicleInCU1.position.rotate/M_PI*180;
                    }
                    else
                    {
//                        globalData->unlock();
                        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("CU1点位载入失败!\n数据获取失败!"));
                        return;
                    }
//                    globalData->unlock();

                }
                // VMark点
                else if(dpoint.pointType == EM_MOVE_FREE_VMARK)
                {
                    // 使用VMark
                    // 上锁获取VMark点位数据
//                    globalData->lock();
                    if(globalData->vehicleInVmark.isUpdateSucess)
                    {
                        dpoint.posR = globalData->vehicleInVmark.position.rotate/M_PI*180;
                        dpoint.posX = globalData->vehicleInVmark.position.x;
                        dpoint.posY = globalData->vehicleInVmark.position.y;
                    }
                    else
                    {
//                        globalData->unlock();
                        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("点位载入失败!\nVMark数据获取失败!"));
                        return;
                    }
//                    globalData->unlock();
                }
                // 二维码点
                else if(dpoint.pointType == EM_MOVE_FREE_QR)
                {
//                    globalData->lock();
                    if(globalData->vehicleInQrcode.isUpdateSucess)
                    {
                        dpoint.posR = globalData->vehicleInQrcode.position.rotate/M_PI*180;
                        dpoint.posX = globalData->vehicleInQrcode.position.x;
                        dpoint.posY = globalData->vehicleInQrcode.position.y;
                    }
                    else
                    {
//                        globalData->unlock();
                        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("二维码点位载入失败!\n数据获取失败!"));
                        return;
                    }
//                    globalData->unlock();
                }
            }


    }
    // 指令属性界面自定义编辑点
    else if(recordType == E_RECORD_EDITED_POINT)
    {
        dpoint = moveFreeInstructionView->moveFreeDPointView->dPointTemp;
    }
    else
    {
        emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tr("载入点失败"));
        return;
    }

    communicateNormalDelegate->getCurrentMapName(dpoint.mapName);

    // 将单位转换位毫米
    convertMetersToMillimeters(dpoint.posX, dpoint.posY);

    qDebug() << "Current Record DPoint Type :" << tmpPointType;
    qDebug() << "Current Record DPoint Index :" << dpoint.dpIndex;
    qDebug() << "Current Record DPoint MapName :" << QString::fromStdString(dpoint.mapName);
    qDebug() << "Current Record DPoint X :" << dpoint.posX;
    qDebug() << "Current Record DPoint Y :" << dpoint.posY;
    qDebug() << "Current Record DPoint R :" << dpoint.posR;

    // 将修改的点位插入到容器中
    fileData.vehicleDPointList.insert(dpoint.dpIndex, dpoint);

    // 将点位数据写入当前程序文件
    fileData.writeProgramFile(fileData.currentFileName, true);
    emit setIsSaveSignal(false);
    tmpPointType+=QString::number(dpoint.dpIndex);
    tmpPointType+=tr("写入成功");
    emit msgBoxSignal(EM_MSG_BOX_SURE_BTN,tr("提示"),tmpPointType);
    emit pointChanged();
}

void MoveFreeInstructionDelegate::moveToDPointSlot(int dpointNum)
{
    Q_UNUSED(dpointNum);
}

void MoveFreeInstructionDelegate::convertMetersToMillimeters(double &meters1, double &meters2)
{
    meters1 *= 1000.000;
    meters2 *= 1000.000;
}




